package com.culture.config.controller;

import java.util.List;

import javax.validation.Valid;

import com.culture.config.common.model.ResponseData;
import com.culture.config.common.util.ResponseUtil;
import com.culture.config.service.MenuService;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api
@RestController
@RequestMapping("/api/menu")
public class MenuController  extends BaseController{
    @Autowired
    private MenuService menuService;
    
    @ApiOperation("page menu")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/page")
    public ResponseData<List<MenuVO>> page(@RequestBody @Valid MenuPageParam param){
        this.authenticationValidate();
        return ResponseUtil.successPage(this.menuService.page(param));
    }

    @ApiOperation("list menu")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/list")
    public ResponseData<List<MenuVO>> list(@RequestBody @Valid MenuSearchPO param){
        this.authenticationValidate();
        return ResponseUtil.success(this.menuService.list(param));
    }    

    @ApiOperation("tree menu")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/tree")
    public ResponseData<MenuTreeVO> tree(@RequestBody MenuTreePO param){
        this.authenticationValidate();
        return ResponseUtil.success(this.menuService.tree(param));
    } 

    @ApiOperation("add menu")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/add")
    public ResponseData<String> add(@RequestBody @Valid MenuAddPO menuAddPO){
        this.authenticationValidate();
        this.menuService.add(menuAddPO);
        return ResponseUtil.success();
    }

    @ApiOperation("update menu")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/update")
    public ResponseData<String> update(@RequestBody @Valid MenuUpdatePO menuUpdatePO){
        this.authenticationValidate();
        this.menuService.update(menuUpdatePO);
        return ResponseUtil.success();
    }

    @ApiOperation("move menu")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/move")
    public ResponseData<String> move(@RequestBody @Valid MenuMovePO menuMovePO){
        this.authenticationValidate();
        this.menuService.move(menuMovePO);
        return ResponseUtil.success();
    }
    
}
